﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Server.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class CommitProject : IService
    {
        /// <summary>
        /// 요청받은 메시지의 본문
        /// </summary>
        CCL.SocketMessage.Request.CommitProject m_body;

        /// <summary>
        /// 새로운 서비스를 생성합니다.
        /// </summary>
        /// <param name="body">요청받은 메시지 본문</param>
        public CommitProject(CCL.SocketMessage.Request.CommitProject body)
        {
            m_body = body;
        }

        /// <summary>
        /// 서비스를 시작합니다.
        /// </summary>
        /// <returns>전송할 byte배열</returns>
        public byte[] Execute()
        {
            CCL.SocketMessage.Response.CommitProject cp = null;
            CCL.SocketMessage.Message msg = null;

            SqlConnection sqlCon = null;
            SqlTransaction trans = null;


            try
            {
                using (sqlCon = new SqlConnection(Properties.Resources.ConnectionString))
                {
                    sqlCon.Open();

                    trans = sqlCon.BeginTransaction();

                    //////////////////////////////////////////////////////////////////////////
                    SqlCommand sqlCmd1 = new SqlCommand(@"
                        UPDATE [Project] 
                        SET headRevision = headRevision + 1
                        WHERE projectId = @pid
                    ", sqlCon, trans);
                    sqlCmd1.Parameters.AddWithValue("@pid", m_body.ProjectID);

                    sqlCmd1.ExecuteNonQuery();

                    //////////////////////////////////////////////////////////////////////////
                    SqlCommand sqlCmd2 = new SqlCommand(@"
                        SELECT headRevision 
                        FROM Project
                        WHERE projectId = @pid
                    ", sqlCon, trans);
                    sqlCmd2.Parameters.AddWithValue("@pid", m_body.ProjectID);

                    SqlDataAdapter da = new SqlDataAdapter(sqlCmd2);
                    DataSet ds = new DataSet();

                    da.Fill(ds);

                    int revision = (int)ds.Tables[0].Rows[0]["headRevision"];

                    //////////////////////////////////////////////////////////////////////////
                    SqlCommand sqlCmd3 = new SqlCommand(@"
                        INSERT INTO [ProjectRevision](ProjectId, revision, userId, dateTime, message)
                        VALUES (@pid, @rev, @uid, @dateTime, @msg)
                    ", sqlCon, trans);
                    sqlCmd3.Parameters.AddWithValue("@pid", m_body.ProjectID);
                    sqlCmd3.Parameters.AddWithValue("@rev", revision);
                    sqlCmd3.Parameters.AddWithValue("@uid", m_body.UserID);
                    sqlCmd3.Parameters.AddWithValue("@dateTime", DateTime.Now.ToShortDateString());
                    sqlCmd3.Parameters.AddWithValue("@msg", m_body.Log);

                    sqlCmd3.ExecuteNonQuery();

                    trans.Commit();

                    cp = new CCL.SocketMessage.Response.CommitProject(revision);
                    msg = CCL.SocketMessage.Message.MessageResponseBuilder(cp);
                }

            }
            catch (System.Exception ex)
            {
                trans.Rollback();

                cp = new CCL.SocketMessage.Response.CommitProject(ex.Message);
                msg = CCL.SocketMessage.Message.MessageResponseBuilder(cp);
            }
            finally
            {
                if (trans != null)
                    trans.Dispose();

                sqlCon.Close();
            }

            return msg.GetByte();
        }
    }
}
